﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CAP.Forms
{
    public partial class OptFloodSetNodeData: UserControl
    {
        #region Attribute
        private DistributedGraph.OptFloodSet.AlgorithmController algorithm;
        #endregion Attribute

        #region Eigenschaften
        /// <summary>
        /// Setzt den AlgorithmusController für das Control.
        /// </summary>
        public DistributedGraph.OptFloodSet.AlgorithmController Algorithm
        {
            set
            {
                algorithm = value;
                // ToDo: Events in Algorithm und Algorithm.Nodes hier defnieren.
            }
        }
        #endregion Eigenschaften

        #region Methoden
        /// <summary>
        /// Instanziert und initialisiert das Control.
        /// </summary>
        public OptFloodSetNodeData()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Aktualisiert das Control unter Zuhilfenahme des markierten Knotens.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        public void Update(DistributedGraph.OptFloodSet.Node selectedNode)
        {
            updateTree(selectedNode);
            updateRound(selectedNode);
            updateConsensus(selectedNode);

            tvSetW.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            tvRound.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblConsensSuccess.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblDecisionSet.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblConsensSuccessDesc.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
            lblDecisionSetDesc.ForeColor = selectedNode != null && selectedNode.FailStops ? Color.Red : Color.Black;
        }



        /// <summary>
        /// Aktualisiert den EIGBaum.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        private void updateTree(DistributedGraph.FloodSet.Node selectedNode)
        {
            tvSetW.Nodes.Clear();
            if (selectedNode != null)
            {
                foreach (int i in selectedNode.LatestRoundValues)
                {
                    tvSetW.Nodes.Add(i.ToString());
                }
            }
        }

        /// <summary>
        /// Aktualisiert den RundenTab.
        /// </summary>
        /// <param name="selectedNode">Der aktuelle Knoten.</param>
        private void updateRound(DistributedGraph.FloodSet.Node selectedNode)
        {
            tvRound.Nodes.Clear();
            if (selectedNode != null)
            {
                foreach (KeyValuePair<string, HashSet<int>> k in selectedNode.ValuesPerNodeInRound)
                {
                    tvRound.Nodes.Add(k.Key, "Knoten: " + k.Key);
                    foreach (int i in k.Value)
                    {
                        tvRound.Nodes[k.Key].Nodes.Add(i.ToString());
                    }
                }
            }

            /* Falls man die "Historie" über alle Runden wissen will:
             * 
            tvRound.Nodes.Clear();
            if (selectedNode != null)
            {
                foreach (KeyValuePair<string, HashSet<int>> k in selectedNode.ValuesPerRound)
                {
                    tvRound.Nodes.Add(k.Key, "Runde: :" + k.Key);
                    foreach (int i in k.Value)
                    {
                        tvRound.Nodes[k.Key].Nodes.Add(i.ToString());
                    }
                }
            }
             */
        }

        /// <summary>
        /// Aktualisiert die Daten über den Konsens.
        /// </summary>
        /// <param name="selectedNode">Der markierte Knoten</param>
        private void updateConsensus(DistributedGraph.FloodSet.Node selectedNode)
        {
            if (selectedNode != null)
            {
                lblConsensSuccess.Text = selectedNode.IsConsensReached ? "Ja" : "Nein";
                lblDecisionSet.Text = selectedNode.IsConsensusComputed ? selectedNode.DecisionResultSet.ToString() : "Ø";
                lblIncorrectDecision.Visible = !selectedNode.IsDecisionCorrect;
            }
            else
            {
                lblConsensSuccess.Text = "-";
                lblDecisionSet.Text = "-";
                lblIncorrectDecision.Visible = false;
            }
        }
        #endregion Methoden
    }
}
